package com.catmiw.swt;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public abstract class BasicDialog extends Dialog{
	protected Shell shell; 
	protected Properties properties;

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public BasicDialog(Shell parent) {
		super(parent);
		// TODO Auto-generated constructor stub
	}

	protected int[] rowposition;
	protected int[] columnposition;

	static int COMPOSITESPACE=4;

	public int messagebox(String title,String message){
		MessageBox mb=new MessageBox(shell);
		mb.setText(title);
		mb.setMessage(message);
		return mb.open();
	}
	
	public void setPosition(Composite composite,int row,int column){
		int sumtop=0,sumleft=0;
		for (int i=0;i<row-1;i++){
			sumtop+=rowposition[i];
		}
		for (int i=0;i<column-1;i++){
			sumleft+=columnposition[i];
		}
		
		setPosition(composite,sumtop,sumleft,sumleft+columnposition[column-1],sumtop+rowposition[row-1]);
	}
	
	public void setPosition(Composite composite,int row){
		int sumtop=0,sumleft=0;
		for (int i=0;i<row-1;i++){
			sumtop+=rowposition[i];
		}
		
		setPosition(composite,sumtop,sumleft,100,sumtop+rowposition[row-1]);
	}
	
	public void setPosition(Composite composite,int top,int left,int right,int bottom){
		FormData data = new FormData(); 
		data.top = new FormAttachment(top, COMPOSITESPACE);
		data.left = new FormAttachment(left, COMPOSITESPACE); 
		data.right = new FormAttachment(right,0-COMPOSITESPACE); 
		data.bottom = new FormAttachment(bottom,0-COMPOSITESPACE); 
		composite.setLayoutData(data);
	}
	
	public Group createGroupRadioChooser(Composite parent,Button[] buttons,String title,String[] options,int style,int defaultChoose){
		Group group=new Group(parent, SWT.SHADOW_OUT);
		group.setText(title);
		group.setLayout(new GridLayout(1,false));
		
		//buttons=new Button[options.length];
		for (int i=0;i<options.length;i++){
			buttons[i]=new Button(group,style);
			buttons[i].setText(options[i]);
			if (defaultChoose==i){
				buttons[i].setSelection(true);
			}
		}
		return group;
	}
	
	public Group createGroupCheckChooser(Composite parent,Button[] buttons,String title,String[] options,int style,int[] defaultChoose){
		Group group=new Group(parent, SWT.SHADOW_OUT);
		group.setText(title);
		group.setLayout(new GridLayout(1,false));
		
		//buttons=new Button[options.length];
		for (int i=0;i<options.length;i++){
			buttons[i]=new Button(group,style);
			buttons[i].setText(options[i]);
			for (int j:defaultChoose){
				if (j==i){
					buttons[i].setSelection(true);
					break;
				}
			}
		}
		return group;
	}
	
	public Combo createComboChooser(Composite parent,String labeltext,int combomode,String[] options,String defaultChoose){
		Composite composite=new Composite(parent, SWT.NONE);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Label label=new Label(composite, SWT.NONE);
		label.setText(labeltext);
		
		Combo combo=new Combo(composite,combomode);

		for (int i=0;i<options.length;i++){
			combo.add(options[i],i);
			if (options[i].equals(defaultChoose)){
				combo.select(i);
			}
		}
		
		return combo;
	}
	
	public void setComboOptions(Combo combo,String options[]){
		for (String s:options){
			combo.add(s);
		}
	}
	
	public void open(){
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL |SWT.MIN);
		shell.setSize(412, 312);
		shell.setText("SWT Dialog");
		
		createContents();
		addListeners();
		
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

	}
	
	public abstract void createContents();
	
	public abstract void addListeners();
	
	public List<String> loadTextFile(String filename,String charset){
		List<String> nctl=new ArrayList<String>();
		
		BufferedReader br=null;
		try {
			br=new BufferedReader(new InputStreamReader(new FileInputStream(filename),charset));
			String str=null;
			while ((str=br.readLine())!=null){
				nctl.add(str);
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if (br!=null)
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		return nctl;
	}
	

}
